﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using Ylh.BLL;
using System.Data;

public partial class Module_StaffManger_RoleTree : PageBase
{
    List<string> li = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        BtnCancel.OnClientClick = ActiveWindow.GetHideReference();

        if (!IsPostBack)
        {
            ComRoleBLL crbll = new ComRoleBLL(DbConnectionStr);
            li = crbll.GetCompetenceByRoleID(Request.QueryString["RoleID"].ToString());
            LoadTv();
        }

    }

    private void LoadTv()
    {
        CompetenceBLL combll = new CompetenceBLL(DbConnectionStr);
        DataTable dt = combll.GetCompetenceByID(0, true);

        ExtAspNet.TreeNode tn;

        foreach (DataRow dr in dt.Rows)
        {
            tn = new ExtAspNet.TreeNode();

            tn.EnableCheckBox = true;

            tn.Text = dr["CompetenceName"].ToString();

            tn.Expanded = true;

            tn.NodeID = dr["CompetenceID"].ToString();


            tn.Checked = CheckCheckBox(tn);


            LoadChildNode(tn);

            Tree1.Nodes.Add(tn);
        }
    }

    private bool CheckCheckBox(ExtAspNet.TreeNode tn)
    {
        if (li.Contains(tn.NodeID.ToString())) return true;
        return false;
    }

    private void LoadChildNode(ExtAspNet.TreeNode tn)
    {
        CompetenceBLL combll = new CompetenceBLL(DbConnectionStr);
        DataTable dt = combll.GetCompetenceByID(Convert.ToDecimal(tn.NodeID), true);

        ExtAspNet.TreeNode tn1;

        foreach (DataRow dr in dt.Rows)
        {
            tn1 = new ExtAspNet.TreeNode();

            tn1.EnableCheckBox = true;

            tn1.Expanded = true;



            tn1.Text = dr["CompetenceName"].ToString();

            tn1.NodeID = dr["CompetenceID"].ToString();


            tn1.Checked = CheckCheckBox(tn1);

            LoadChildNode(tn1);

            tn.Nodes.Add(tn1);

        }

    }

    protected void BtnComfir_Click(object sender, EventArgs e)
    {
        ComRoleBLL combll = new ComRoleBLL(DbConnectionStr);

        if (!combll.InserComByRold(Tree1.GetCheckedNodeIDs(), Request.QueryString["RoleID"].ToString()))
        {
            Alert.Show("权限修改失败,请与管理员联系");
            return;
        } ;

        PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
    }
}